// test_cache.c
#include "test_framework.h"
#include "../hisi_l0_mem_pool.h"
#include "test_common.h"
#include "test_registration.h"

/**
 * test_cache_level_initialization - Tests Fix Allocator cache level initialization
 * 
 * This test verifies:
 * 1. Proper initialization of Fix Allocator cache levels through public API
 * 2. Minimum object size (32 bytes) allocation
 * 3. Cache system initialization process
 * 
 * Tests basic cache level functionality and initialization
 */
static int test_cache_level_initialization(struct seq_file *m)
{
    void *ptr;
    struct test_context *ctx = get_test_context();
    int passed = 0, total = 0;
    
    // Test that cache levels work through public API
    total++;
    ptr = l0_kmalloc(32, ctx->test_node_id); // Minimum size
    if (ptr) {
        // Verify memory is accessible
        memset(ptr, 0xAA, 32);
        l0_kfree(ptr);
        TEST_PASS(m, "Cache level initialization (32 bytes) successful");
        passed++;
    } else {
        TEST_FAIL(m, "Cache level initialization (32 bytes) failed");
    }
    
    TEST_END(m, "Cache level initialization tests: %d/%d passed", passed, total);
    return (passed == total) ? 0 : -1;
}

/**
 * test_cache_level_allocation - Tests allocation at different cache levels
 * 
 * This test verifies:
 * 1. Allocation at different cache levels (32-4096 bytes)
 * 2. Memory accessibility for all cache sizes
 * 3. Proper cleanup of cache-allocated objects
 * 4. Functionality of all cache levels
 * 
 * Tests comprehensive cache allocation functionality
 */
static int test_cache_level_allocation(struct seq_file *m)
{
    struct test_context *ctx = get_test_context();
    void *ptrs[5];
    int i;
    size_t sizes[] = {32, 64, 128, 256, 512, 1024, 2048, 4096};
    int passed = 0, total = 0;
    
    // Test allocation at different cache levels through public API
    for (i = 0; i < ARRAY_SIZE(sizes) && i < 5; i++) {
        total++;
        ptrs[i] = l0_kmalloc(sizes[i], ctx->test_node_id);
        if (ptrs[i]) {
            memset(ptrs[i], 0xAA, sizes[i]);
            TEST_PASS_DEBUG(m, "Cache level size %zu allocation successful", sizes[i]);
            passed++;
        } else {
            TEST_FAIL(m, "Cache level size %zu allocation failed", sizes[i]);
        }
    }
    
    // Free all allocated memory
    for (i = 0; i < ARRAY_SIZE(sizes) && i < 5; i++) {
        if (ptrs[i]) {
            l0_kfree(ptrs[i]);
        }
    }
    
    TEST_END(m, "Cache level allocation tests: %d/%d passed", passed, total);
    return (passed == total) ? 0 : -1;
}

// Export test cases
static struct test_case test_cache_cases[] = {
    {"Cache Level Initialization", test_cache_level_initialization},
    {"Cache Level Allocation", test_cache_level_allocation},
};

DECLARE_TEST_MODULE(cache, test_cache_cases)